/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tbenglish_client.form;

import bllInterface.ChatMessage;
import bllInterface.ChatMessageContent;
import bllInterface.RoomChatManager;
import bllInterface.iChatUser;
import bllInterface.iRoomListener;
import bllfactory.SBLLFactory;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.UnsupportedEncodingException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import model.ClientModel;
import model.chat.ChatMessageModel;
import model.rooms.RoomChatModel;
import model.rooms.RoomListenerImpl;

/**
 *
 * @author Mrkupi
 */
public class worldChat extends javax.swing.JFrame implements ListDataListener {

    private iChatUser user;
    private String roomID;
    private iRoomListener roomListener;
    
    public worldChat() {
        roomID = "0";
        
        initComponents();
        setLocation(50, 50);

        windowInitCustomize();
        loginToServerChat();
        loadUserInfo();
        loadRoomHistory();
    }

    protected void windowInitCustomize() {
        setResizable(false);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
               dispose();
            }
        });
    }

    protected void loginToServerChat() {
        // login to server chat
        try {
            ChatMessageModel msgModel = new ChatMessageModel();
            user = ClientModel.getInstance().getChatUser();
            SBLLFactory.getInstance().getServerChat().login(user);
            msgModel.addListDataListener(this);
            ClientModel.getInstance().getChatMsgModels().put("0", msgModel);
            bllfactory.SBLLFactory.getInstance().getChat().getRoom("0").join(user);

            // try to get roomlist
            lstRooms.setModel(new DefaultListModel());

            this.roomListener = new RoomListenerImpl();
            ((RoomListenerImpl) this.roomListener).addListListener(this);
            RoomChatManager chat = bllfactory.SBLLFactory.getInstance().getChat();
            chat.addRoomListener(this.roomListener);
        } catch (RemoteException ex) {
            System.out.println("Can't login :D");
            Logger.getLogger(worldChat.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    protected void loadUserInfo() {
        try {
            this.lblNickname.setText(new String(ClientModel.getInstance().getAccount().getUserName().getBytes(), "UTF-8"));

            this.lblExp.setText(String.valueOf(ClientModel.getInstance().getAccount().getExp()));
            this.lblLevel.setText(String.valueOf(ClientModel.getInstance().getAccount().getLevel()));
        } catch (RemoteException | UnsupportedEncodingException ex) {
            Logger.getLogger(userInfo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void dispose(){ 
        try {
            SBLLFactory.getInstance().getServerChat().logout(ClientModel.getInstance().getChatUser());
            RoomChatManager chat = bllfactory.SBLLFactory.getInstance().getChat();
            chat.removeRoomListener(roomListener);
            
            SBLLFactory.getInstance().getChat().getRoom(roomID).leave(user);
            System.out.println("Leave Success");
            
            if( RoomContainer.Container.containsKey( "World" ))
                RoomContainer.Container.remove( "World" );
            
        } catch (RemoteException ex) {
            System.out.println("Leave Fail");
            Logger.getLogger(worldChat.class.getName()).log(Level.SEVERE, null, ex);
        }
        super.dispose();
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        pnInfo = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        lblLevel = new javax.swing.JLabel();
        lblExp = new javax.swing.JLabel();
        lblNickname = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        taChatBox = new javax.swing.JTextArea();
        tfChat = new javax.swing.JTextField();
        btEnter = new javax.swing.JButton();
        pnLearn = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        btCreateRoom = new javax.swing.JButton();
        tfRoomID = new javax.swing.JTextField();
        btJoinRoom = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstRooms = new javax.swing.JList();
        jLabel8 = new javax.swing.JLabel();

        jLabel2.setText("jLabel2");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("WELCOME TO WORLD CHAT");

        pnInfo.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel4.setText("Level");

        jLabel5.setText("Exp");

        jLabel6.setText("Nick name");

        lblLevel.setText("jLabel8");

        lblExp.setText("jLabel8");

        lblNickname.setText("jLabel8");

        jButton2.setIcon(new javax.swing.ImageIcon("E:\\Projects\\Java\\tbenglish\\TBEnglish_Client\\src\\tbenglish_client\\form\\Image\\HEINS_DESKTOP_LOOKUPS BRUCE.png")); // NOI18N

        javax.swing.GroupLayout pnInfoLayout = new javax.swing.GroupLayout(pnInfo);
        pnInfo.setLayout(pnInfoLayout);
        pnInfoLayout.setHorizontalGroup(
            pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnInfoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnInfoLayout.createSequentialGroup()
                        .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblLevel)
                            .addComponent(lblExp)
                            .addComponent(lblNickname)))
                    .addGroup(pnInfoLayout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        pnInfoLayout.setVerticalGroup(
            pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnInfoLayout.createSequentialGroup()
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(lblLevel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(lblExp))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(lblNickname))
                .addGap(4, 4, 4))
        );

        taChatBox.setEditable(false);
        taChatBox.setColumns(20);
        taChatBox.setRows(5);
        taChatBox.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jScrollPane1.setViewportView(taChatBox);

        tfChat.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                onEnter_pressed(evt);
            }
        });

        btEnter.setText("Enter");
        btEnter.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                onEnter_clicked(evt);
            }
        });

        pnLearn.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout pnLearnLayout = new javax.swing.GroupLayout(pnLearn);
        pnLearn.setLayout(pnLearnLayout);
        pnLearnLayout.setHorizontalGroup(
            pnLearnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        pnLearnLayout.setVerticalGroup(
            pnLearnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("ROOM");
        jLabel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("LEARN");
        jLabel7.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btCreateRoom.setIcon(new javax.swing.ImageIcon("E:\\Projects\\Java\\tbenglish\\TBEnglish_Client\\src\\tbenglish_client\\form\\Image\\FIRST AID.png")); // NOI18N
        btCreateRoom.setText("create Room");
        btCreateRoom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCreateRoomActionPerformed(evt);
            }
        });

        btJoinRoom.setIcon(new javax.swing.ImageIcon("E:\\Projects\\Java\\tbenglish\\TBEnglish_Client\\src\\tbenglish_client\\form\\Image\\Gloss PNGChange_User.png")); // NOI18N
        btJoinRoom.setText("join Room");
        btJoinRoom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btJoinRoomActionPerformed(evt);
            }
        });

        lstRooms.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                roomList_onClicked(evt);
            }
        });
        jScrollPane2.setViewportView(lstRooms);

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel8.setText("Room Name");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(pnInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btCreateRoom, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btJoinRoom, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfRoomID, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tfChat, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btEnter, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(pnLearn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(230, 230, 230))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 323, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfChat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btEnter))
                        .addGap(1, 1, 1))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnLearn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfRoomID)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btCreateRoom)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btJoinRoom)
                        .addGap(41, 41, 41)))
                .addGap(26, 26, 26))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void onEnter_pressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_onEnter_pressed
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
            sendMessageToAllPeople();
        }
    }//GEN-LAST:event_onEnter_pressed

    private void onEnter_clicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_onEnter_clicked
        sendMessageToAllPeople();
    }//GEN-LAST:event_onEnter_clicked

    private void btCreateRoomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCreateRoomActionPerformed
        createRoomChat(tfRoomID.getText());
    }//GEN-LAST:event_btCreateRoomActionPerformed

    private void btJoinRoomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btJoinRoomActionPerformed
        //joinWorldChat(tfRoomID.getText(), false);
    }//GEN-LAST:event_btJoinRoomActionPerformed

    private void roomList_onClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_roomList_onClicked
        if (evt.getClickCount() == 2) {
            Object selected =   lstRooms.getSelectedValue();
            String strRoom  =   (String) selected;
            String[] arr    =   strRoom.split(":");
            if( arr.length >= 2 )
                joinRoomChat(arr[0], arr[1], false);
        }
    }//GEN-LAST:event_roomList_onClicked

    private boolean createRoomChat(String roomName) {
        try {
            this.roomID = SBLLFactory.getInstance().getChat().createRoomChat( roomName );
            return this.joinRoomChat(roomID, roomName, true);
        } catch (RemoteException ex) {
            Logger.getLogger(worldChat.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    private boolean joinRoomChat(String id, String roomName, boolean isBoss) {
        if( !RoomContainer.Container.containsKey(id) )
        {
            try {
                ChatMessageModel chatMsgModel = new ChatMessageModel();
                ClientModel.getInstance().getChatMsgModels().put(id, chatMsgModel);
                if (bllfactory.SBLLFactory.getInstance().getChat().getRoom(id).join(user)) {
                    try {
                        roomChat room = new roomChat(ClientModel.getInstance().getChatMsgModels().get(id), id, roomName, isBoss);
                        room.setVisible(true);
                        RoomContainer.Container.put(id, room);
                    } catch (Exception ex) {
                        Logger.getLogger(worldChat.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    return true;
                }
                return false;
            } catch (RemoteException ex) {
                System.out.println("Can't join room id :D");
                Logger.getLogger(worldChat.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return false;
    }

    private boolean sendMessageToAllPeople() {
        if (!makeSureNotEmptyTextField()) {
            return false;
        }
        try {
            // put code send message
            ChatMessage cm = ChatMessage.createMessage(ClientModel.getInstance().getAccount().getUserID(), tfChat.getText(), this.roomID);
            SBLLFactory.getInstance().getServerChat().post(cm);
            tfChat.setText("");
            return true;
        } catch (RemoteException ex) {
            System.out.println("Can't send message to all people :D");
            Logger.getLogger(worldChat.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    private boolean makeSureNotEmptyTextField() {
        if ("".equals(tfChat.getText())) {
            JOptionPane.showMessageDialog(this, "");
            return false;
        }
        return true;
    }

    private void publishNewMessageToTextArea(ChatMessage message) {
        this.taChatBox.append("\n" + String.format("%s: %s", message.getContent().get(ChatMessageContent.ORIGINAL_SENDER), message.getContent().get(ChatMessageContent.MESSAGE_CONTENT)));
    }
    
    protected void loadRoomHistory() {
        try {
            ArrayList<ChatMessage> messages = bllfactory.SBLLFactory.getInstance().getChat().getRoom(roomID).getHistory();
            for (ChatMessage cm : messages) {
                this.taChatBox.append("\n" + String.format("%s: %s", cm.getSender(), cm.getContent().get(ChatMessageContent.MESSAGE_CONTENT)));
            }
        } catch (RemoteException ex) {
            System.out.println("Error load history");
        }
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCreateRoom;
    private javax.swing.JButton btEnter;
    private javax.swing.JButton btJoinRoom;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblExp;
    private javax.swing.JLabel lblLevel;
    private javax.swing.JLabel lblNickname;
    private javax.swing.JList lstRooms;
    private javax.swing.JPanel pnInfo;
    private javax.swing.JPanel pnLearn;
    private javax.swing.JTextArea taChatBox;
    private javax.swing.JTextField tfChat;
    private javax.swing.JTextField tfRoomID;
    // End of variables declaration//GEN-END:variables

    @Override
    public void intervalAdded(ListDataEvent e) {
        if (e.getSource() instanceof ChatMessageModel) {
            ChatMessageModel chatMsgModel = (ChatMessageModel) e.getSource();
            try {
                for (int i = e.getIndex0(); i < e.getIndex1(); ++i) {
                    ChatMessage cm = (ChatMessage) chatMsgModel.getElementAt(i);
                    if (cm.getSender() == null ? ClientModel.getInstance().getAccount().getUserID() != null : !cm.getSender().equals(ClientModel.getInstance().getAccount().getUserID())) {
                        this.publishNewMessageToTextArea(cm);
                    }
                }
            } catch (RemoteException ex) {
                Logger.getLogger(roomChat.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (e.getSource() instanceof RoomChatModel) {
            RoomChatModel roomModel = (RoomChatModel) e.getSource();
            DefaultListModel listModel = (DefaultListModel) lstRooms.getModel();
            for (int i = e.getIndex0(); i < e.getIndex1(); ++i) {
                listModel.addElement(roomModel.getElementAt(i));
            }
            lstRooms.setModel(listModel);
        }
    }

    @Override
    public void intervalRemoved(ListDataEvent e) {
    }

    @Override
    public void contentsChanged(ListDataEvent e) {
    }
}
